﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class System_ProductSortSelect : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strSql;
        Db db = new Db();
        System.Data.IDataReader dr;

        strSql = "Select * From product_sort";
        dr = db.DataReader(strSql);
        while (dr.Read())
        {
            int intFrontSortId = SysFun.ToInt(dr["FrontSortId"]);

            product_sort _product_sort = new product_sort();
            _product_sort.SortId = SysFun.ToInt(dr["SortId"]);
            _product_sort.SortName = SysFun.ToTrim(dr["SortName"]);

            if (intFrontSortId == 0)
            {
                this.ChildrenSorts.Add(_product_sort);
            }
            else
            {
                product_sort _FrontSort = FindFrontSort(intFrontSortId, this.ChildrenSorts);
                if (_FrontSort != null)
                {
                    _FrontSort.ChildrenSorts.Add(_product_sort);
                }
            }
        }
        dr.Close();

        db.Close();

        labProductSort.Text = OutSort(this.ChildrenSorts);
    }

    private string OutSort(System.Collections.Generic.List<product_sort> childrenSorts)
    {
        string strValue = "";
        foreach (product_sort _product_sort in childrenSorts)
        {
            string strChildrenValue = OutSort(_product_sort.ChildrenSorts);
            strValue += "<span style='behavior:url(../System/ProductSortSelect.htc)' SortId='" + _product_sort.SortId + "' SortName='" + _product_sort.SortName + "'></span>";
            strValue += "<div style='margin-Left:16;display:none'>" + strChildrenValue + "</div>";
        }
        return strValue;
    }

    private product_sort FindFrontSort(int frontSortId, System.Collections.Generic.List<product_sort> childrenSorts)
    {
        foreach (product_sort _product_sort in childrenSorts)
        {
            if (_product_sort.SortId == frontSortId)
            {
                return _product_sort;
            }
            else if (_product_sort.ChildrenSorts.Count > 0)
            {
                product_sort _sort = FindFrontSort(frontSortId, _product_sort.ChildrenSorts);
                if (_sort != null)
                {
                    return _sort;
                }
            }
        }
        return null;
    }

    private System.Collections.Generic.List<product_sort> m_ChildrenSorts = new System.Collections.Generic.List<product_sort>();
    /// <summary>
    /// 子分类

    /// </summary>
    public System.Collections.Generic.List<product_sort> ChildrenSorts
    {
        get { return m_ChildrenSorts; }
        set { m_ChildrenSorts = value; }
    }


    public class product_sort
    {
        private int m_SortId;

        public int SortId
        {
            get { return m_SortId; }
            set { m_SortId = value; }
        }

        private string m_SortName;

        public string SortName
        {
            get { return m_SortName; }
            set { m_SortName = value; }
        }

        private System.Collections.Generic.List<product_sort> m_ChildrenSorts = new System.Collections.Generic.List<product_sort>();
        /// <summary>
        /// 子分类

        /// </summary>
        public System.Collections.Generic.List<product_sort> ChildrenSorts
        {
            get { return m_ChildrenSorts; }
            set { m_ChildrenSorts = value; }
        }

    }
}
